<template>
  <div>
    <h3>登录页面</h3>
    <el-form :model="ruleLogin" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="width:500px;margin:0 auto">
        <el-form-item label="用户名" prop="username">
            <el-input  v-model="ruleLogin.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleLogin.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
            <el-input v-model="ruleLogin.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form-item>
</el-form>

  </div>
</template>

<script>
import axios from 'axios'
  export default {
    data() {
      var validateUser =(rule, value, callback)=>{
        if (value === '') {
          callback(new Error('请输入用户名'));
        }
            const userRules=/^[a-zA-Z][a-zA-Z0-9_]\w{4,15}$/
            if(userRules.test(value)){
                return callback()
            }else{
                return callback('用户名格式不正确')
            }
      };

      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }
        const passRules=/^[a-zA-Z][a-zA-Z0-9]{4,15}$/
        if(passRules.test(value)){
            return callback();
        }else{
            return callback(new Error('密码格式错误'));
        }
      };
      var validateMobile =(rule, value, callback)=>{
        if (value === '') {
          callback(new Error('请输入手机号'));
        }
            const userRules=/^1[3-9][0-9]{9}$/
            if(userRules.test(value)){
                return callback()
            }else{
                return callback('手机号格式不正确')
            }
      };

      return {
        ruleLogin: {
          username:'',
          password: '',
          mobile: '',
        },
        rules: {
          username: [
            { validator: validateUser, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          mobile: [
            { validator: validateMobile, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post('user/login/',this.ruleLogin)
            .then(resp=>{
                console.log(resp);
                if(resp.data.code == 200){
                    alert('登录成功')
                    // 保存用户信息
                    this.$store.commit('setUser',resp.data.user)
                    let user_json=JSON.stringify(resp.data.user)  //转换为json字符串
                //   长期 保存到浏览器
                    localStorage.setItem('user',user_json)
                    // 保存token信息
                    localStorage.setItem('token',resp.data.token)
                    this.$router.push('/index')
                }else{
                    alert(resp.data.msg)
                }
            })
            .catch(err=>{
                console.log(err);
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>

</style>